package com.yaorange.nongjt.certificate.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yaorange.nongjt.common.annotation.Log;
import com.yaorange.nongjt.common.core.controller.BaseController;
import com.yaorange.nongjt.common.core.domain.AjaxResult;
import com.yaorange.nongjt.common.enums.BusinessType;
import com.yaorange.nongjt.certificate.domain.NjtArea;
import com.yaorange.nongjt.certificate.service.INjtAreaService;
import com.yaorange.nongjt.common.utils.poi.ExcelUtil;
import com.yaorange.nongjt.common.core.page.TableDataInfo;

/**
 * 地区层级关系Controller
 * 
 * @author ruoyi
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/certificate/area")
/*//原生代码
@RequestMapping("/certificate/bjt_area")*/
public class NjtAreaController extends BaseController
{
    @Autowired
    private INjtAreaService njtAreaService;

//    查询省列表
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:list')")
    @GetMapping("/list/root")
    public AjaxResult listRoot()
    {
//        List<NjtArea> list = njtAreaService.selectNjtAreaList(new NjtArea());
        List<NjtArea> list = njtAreaService.selectNjtAreaListRoot();

        return success(list);
    }

//根据查询省列表的参数查询市列表
//    certificate/area/childrenlist/510000999
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:list')")
    @GetMapping("/childrenlist/{parentCode}")
    public Map<String, Object> getChildrenList(@PathVariable String parentCode) {
//        返回map类型
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", njtAreaService.getChildrenByParentCode(parentCode));
        return map;
    }

    /**
     * 查询地区层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtArea njtArea)
    {
        startPage();
        List<NjtArea> list = njtAreaService.selectNjtAreaList(njtArea);
        return getDataTable(list);
    }

    /**
     * 导出地区层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:export')")
    @Log(title = "地区层级关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtArea njtArea)
    {
        List<NjtArea> list = njtAreaService.selectNjtAreaList(njtArea);
        ExcelUtil<NjtArea> util = new ExcelUtil<NjtArea>(NjtArea.class);
        util.exportExcel(response, list, "地区层级关系数据");
    }

    /**
     * 获取地区层级关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(njtAreaService.selectNjtAreaById(id));
    }

    /**
     * 新增地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:add')")
    @Log(title = "地区层级关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtArea njtArea)
    {
        return toAjax(njtAreaService.insertNjtArea(njtArea));
    }

    /**
     * 修改地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:edit')")
    @Log(title = "地区层级关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtArea njtArea)
    {
        return toAjax(njtAreaService.updateNjtArea(njtArea));
    }

    /**
     * 删除地区层级关系
     */
    @PreAuthorize("@ss.hasPermi('certificate:njt_area:remove')")
    @Log(title = "地区层级关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(njtAreaService.deleteNjtAreaByIds(ids));
    }
}
